package com.biovr.backend.restcontroller;

import com.biovr.backend.domain.BroadMessage;
import com.biovr.backend.repository.BroadMessageRepository;
import com.biovr.backend.utils.ResponseFactory;
import com.biovr.backend.utils.RestResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * Created by monodev on 2017/6/10.
 */

@Api(value = "/broads",description = "广播消息", consumes="application/json")
@RestController
@RequestMapping("/broads")
public class BroadMessageRestController {
    @Autowired
    private BroadMessageRepository broadMessageRepository;

    @ApiOperation(value="获得分页并根据发布日期排序的广播消息",response = BroadMessage.class,responseContainer = "List")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageIndex", value = "页码", required = true, dataType = "string",paramType = "path"),
            @ApiImplicitParam(name = "pageNumber", value = "每页数量", required = true, dataType = "string",paramType = "path")
    })

    @GetMapping("/{pageIndex}/{pageNumber}")
    public RestResponse<List<BroadMessage>> getAll(@PathVariable String pageIndex,@PathVariable String pageNumber){
        int p = Integer.parseInt(pageIndex);
        int n = Integer.parseInt(pageNumber);
        //构建分页信息
        PageRequest pageRequest = buildPageRequest(p,n,null);
        //查询指定分页的内容
        Iterator<BroadMessage> broads =  broadMessageRepository.findAll(pageRequest).iterator();
        List<BroadMessage> lists = new ArrayList<BroadMessage>();
        while(broads.hasNext()){
            lists.add(broads.next());
        }
        return  ResponseFactory.createSuccessResult(lists);
    }

    @ApiOperation(value="添加广播消息",response = BroadMessage.class)
    @ApiImplicitParam(name = "broadMessage", value = "广播消息类", required = true, dataType = "BroadMessage")
    @PostMapping
    public  RestResponse<BroadMessage> addMsg(@RequestBody BroadMessage broadMessage) throws  Exception{
        BroadMessage msg  = broadMessageRepository.save(broadMessage);
        return  ResponseFactory.createSuccessResult(msg);
    }


    /**
     *   //参数1表示当前第几页,参数2表示每页的大小,参数3表示排序
     * @param pageNumber
     * @param pageSize
     * @param sortType
     * @return
     */
    private PageRequest buildPageRequest(int pageNumber, int pageSize,String sortType) {
        Sort sort = null;
        if ("auto".equals(sortType)) {
            sort = new Sort(Sort.Direction.ASC, "_id");
        } else if ("date".equals(sortType)) {
            sort = new Sort(Sort.Direction.DESC, "sendTime");
        }

        return new PageRequest(pageNumber-1,pageSize,sort);
    }
}
